from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from type_tutor.models import Login
from type_tutor.models import Article
from type_tutor.models import Detail

import json

# Create your views here.
def index(request):
	return HttpResponse("hello type tutor")

def login(request):
	#print(request)

	action=request.GET.get('action')
	u=request.GET.get('u')
	p=request.GET.get('p')
	r="";
	response = {}
	#http://127.0.0.1:8000/typetutor/login/?action=login&u=zs&p=aaa
	if( action=="login" ):
		try:
			user_obj=Login.objects.get( uname=u )
		except:
			#r="user not exist"
			response['msg'] = 'user not exist'
			response['error_num'] = 2
		else:
			if( p==user_obj.pswd ):
				#r="login ok"
				response['msg'] = 'login ok'
				response['error_num'] = 0
			else:
				#r="username password do not matched"
				response['msg'] = 'username password do not matched'
				response['error_num'] = 1
	
	#http://127.0.0.1:8000/typetutor/login/?action=register&u=zs&p=aaa
	if( action=="register" ):
		if Login.objects.filter( uname=u ).exists():
			#r="username already exist"
			response['msg'] = 'username already exist'
			response['error_num'] = 1
		else:
			Login.objects.create( uname=u,pswd=p )
			#r="register ok"
			response['msg'] = 'register ok'
			response['error_num'] = 0
	#return HttpResponse(r)
	return JsonResponse(response)

def article(request):
	r="";
	action=request.GET.get('action')
	#http://127.0.0.1:8000/typetutor/article/?action=get_all_title
	if( action=='get_all_title' ):
		a=[]
		for itm in Article.objects.all():
			a.append(itm.biao_ti)
		r="$".join(a)
	#http://127.0.0.1:8000/typetutor/article/?action=get_by_title&biao_ti=test01
	if( action=='get_by_title' ):
		bt=request.GET.get('biao_ti')
		try:
			article_obj=Article.objects.get( biao_ti=bt )
		except:
			r="article not find"
		else:
			r=article_obj.nei_rong
	return HttpResponse(r)

#return json
def article2(request):
	r="";
	action=request.GET.get('action')
	#http://127.0.0.1:8000/typetutor/article2/?action=get_all_title
	if( action=='get_all_title' ):
		a=[]
		for itm in Article.objects.all():
			a.append(itm.biao_ti)
		#r="$".join(a)
		j={ "titles" : a }
		
	#http://127.0.0.1:8000/typetutor/article2/?action=get_by_title&biao_ti=test01
	if( action=='get_by_title' ):
		bt=request.GET.get('biao_ti')
		try:
			article_obj=Article.objects.get( biao_ti=bt )
		except:
			nr="article not find"
		else:
			nr=article_obj.nei_rong
		j={ 'title': bt, 'content': nr }
	
	r=json.dumps(j)
	#return HttpResponse(r)
	return HttpResponse(r, content_type="application/json")

def detail(request):
	r=''
	action=request.GET.get('action')
	#http://127.0.0.1:8000/typetutor/detail/?action=start&uname=zs&biao_ti=test01&nan_du=2&date_time=1810241030
	if( action=='start' ):
		cs_uname=request.GET.get('uname')
		cs_biao_ti=request.GET.get('biao_ti')
		cs_nan_du=request.GET.get('nan_du')
		cs_date_time=request.GET.get('date_time')
		Detail.objects.create( uname=cs_uname,biao_ti=cs_biao_ti,nan_du=cs_nan_du,start_time=cs_date_time )
		r='start ok'
	#http://127.0.0.1:8000/typetutor/detail/?action=stop&uname=zs&biao_ti=test01&date_time=1810241030&date_time2=1810241033
	if( action=='stop' ):
		cs_uname=request.GET.get('uname')
		cs_biao_ti=request.GET.get('biao_ti')
		cs_date_time=request.GET.get('date_time')
		cs_date_time2=request.GET.get('date_time2')
		detail_obj=Detail.objects.get( uname=cs_uname,biao_ti=cs_biao_ti )
		detail_obj.end_time=cs_date_time2
		detail_obj.save()
		r='stop ok'
	return HttpResponse(r)